package com.timbo.service.system.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import com.timbo.constant.CacheConstant;
import com.timbo.dao.sms.SysSmsDao;
import com.timbo.dao.system.SysConfigDao;
import com.timbo.dao.system.SystemCommonDao;
import com.timbo.dao.system.SysOperationDao;
import com.timbo.dao.system.SysTypeDao;
import com.timbo.dao.system.SysTypeGroupDao;
import com.timbo.model.system.SysConfig;
import com.timbo.model.system.SysOperation;
import com.timbo.model.system.SysSms;
import com.timbo.model.system.SysType;
import com.timbo.model.system.SysTypeGroup;
import com.timbo.service.system.ISystemSV;

@Service("systemSV")
public class SystemSVImpl implements ISystemSV {
	
	@Autowired
	private SysTypeDao<SysType> sysTypeDao;
	@Autowired
	private SysTypeGroupDao<SysTypeGroup> sysTypeGroupDao;
	@Autowired
	private SystemCommonDao systemCommonDao;
	@Autowired
	private SysConfigDao<SysConfig> sysConfigDao;
	@Autowired
	private SysSmsDao<SysSms> sysSmsDao;
	@Autowired
	private SysOperationDao<SysOperation> sysOperationDao;
	@Autowired
	private StringRedisTemplate stringRedisTemplate;

	@Override
	public String getLang(String langKey) throws Exception {
		
		ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();
		String langCode = operations.get(CacheConstant.GLOBAL_SYSTEM_CONSTANT
			+ CacheConstant.SEPARATOR_KEY+"lang_code");
		String langContext = operations.get(CacheConstant.SYS_MULT_LANG 
			+ CacheConstant.SEPARATOR_KEY + langKey + CacheConstant.SEPARATOR_KEY + langCode);
		
		return StringUtils.isBlank(langContext)? langKey: langContext;
	}
	
//	private Map<String, String> jsonToMap(String json) {
//		
//		Map<String, String> map = new HashMap<String, String>();
//		JSONArray arr = JSONArray.parseArray(json);
//		for (int idx=0; idx<arr.size(); idx++) {
//			JSONObject jsonObj = arr.getJSONObject(idx);
//			String key = String.valueOf(jsonObj.keySet().toArray()[0]);
//			String value = jsonObj.getString(key);
//			map.put(key, value);
//		}
//		
//		return map;
//	}

	@Override
	public String getLang(String langKey, String langArg) throws Exception {
		
		ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();
		String langCode = operations.get(CacheConstant.GLOBAL_SYSTEM_CONSTANT
			+ CacheConstant.SEPARATOR_KEY+"lang_code");
		String langContext = operations.get(CacheConstant.SYS_MULT_LANG 
			+ CacheConstant.SEPARATOR_KEY + langKey + CacheConstant.SEPARATOR_KEY + langCode);
		
		if (StringUtils.isBlank(langArg)) {
			return StringUtils.isBlank(langContext)? langKey: langContext;
		}
		String[] langArray = langArg.split(",");
		for (int idx=0; idx<langArray.length; idx++) {
			String langArgContext = operations.get(CacheConstant.SYS_MULT_LANG
				+ CacheConstant.SEPARATOR_KEY + langArray[idx] + CacheConstant.SEPARATOR_KEY + langCode);
			langContext = langContext.replaceAll("\\{"+idx+"\\}", langArgContext);
		}
		
		return langContext;
	}

	@Override
	public SysTypeGroup querySysTypeGroupByCode(String typegroupcode) {
		
		SysTypeGroup sysTypeGroup = sysTypeGroupDao.queryByCode(typegroupcode);
		if (sysTypeGroup != null) {
			List<SysType> sysTypes = sysTypeDao.queryAllByTypegroupid(sysTypeGroup.getId());
			sysTypeGroup.setSysTypes(sysTypes);
		}
		
		return sysTypeGroup;
	}

	@Override
	public List<Map<String, String>> queryDic(String dictField, String dictText, String dictTable) {
		
		StringBuffer sqlSb = new StringBuffer();
		sqlSb.append("SELECT ").append(dictField).append(" dictField, ")
			.append(dictText).append(" dictText ").append("FROM ")
			.append(dictTable);
		Map<String, String> map = new HashMap<String, String>();
		map.put("sqlDict", sqlSb.toString());
		List<Map<String, String>> mapList = systemCommonDao.queryDic(map);
		return mapList;
	}

	@Override
	public List<SysConfig> queryAllSysConfig() {
		return sysConfigDao.queryAll();
	}

	@Override
	public List<SysSms> queryMsgs(String esReceiver, String esType, String esSendtime) throws ParseException {
		
		SysSms sysSms = new SysSms();
		sysSms.setEsReceiver(esReceiver);
		sysSms.setEsType(esType);
		sysSms.setEsSendtime(new SimpleDateFormat("yyyy-MM-dd").parse(esSendtime));
		List<SysSms> sysSmss = sysSmsDao.queryMsgs(sysSms);
		return sysSmss;
	}

	@Override
	public SysOperation queryOperationByCode(String operationCode) {
		
		SysOperation sysOperation = sysOperationDao.queryOperationByCode(operationCode);
		return sysOperation;
	}
	
}
